package com.im.service.fundPlan.smo;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.github.pagehelper.util.StringUtil;
import com.im.service.common.smo.ICommonSMO;
import com.im.service.dto.InvestPlan;
import com.im.service.investPlan.dao.IInvestPlanOperDAO;
import com.im.service.investPlan.dao.IInvestPlanQueryDAO;
import com.im.service.investPlan.smo.IInvestPlanOperSMO;
import com.im.service.model.PlanGreenReqVo;
import com.im.service.planDeclareSet.dao.IPlanDeclareSetQueryDAO;
import com.im.util.ReturnsUtil;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.im.MDA;
import com.im.service.dto.FundPlan;
import com.im.service.dto.FundSource;
import com.im.service.dto.PlanDeclareSet;
import com.im.service.fundPlan.dao.IFundPlanOperDAO;
import com.im.service.planDeclareSet.smo.IPlanDeclareSetOperSMO;
import com.im.service.planDeclareSet.smo.IPlanDeclareSetQuerySMO;
import com.im.service.projectInfo.dao.IFundSourceOperDAO;
import com.im.util.DateUtil;
import com.im.util.JsonUtil;
import com.im.util.ListUtil;
import com.im.viewmodel.fundPlan.FundPlanListRespVo;

import net.sf.json.JSONObject;

@Service
public class FundPlanOperSMOImpl implements IFundPlanOperSMO {



	@Autowired
	private IFundPlanOperDAO fundPlanOperDAO;
	@Autowired
	private IFundPlanQuerySMO fundPlanQuerySMO;
	@Autowired
	private IPlanDeclareSetQuerySMO planDeclareSetQuerySMO;
	@Autowired
	private IFundSourceOperDAO fundSourceOperDAO;


	
	@Override
	public void saveFundPlanInfo(String jsonStr) throws Exception {
		JSONObject jo = JSONObject.fromObject(jsonStr);
		
		// 保存  planDeclareSet
		String fundPlanName = JsonUtil.getNotNullStringFromJSON(jo,"fundPlanName","解析");
		//String enterRequest = JsonUtil.getNotNullStringFromJSON(jo,"enterRequest","解析");
		String docCode = JsonUtil.getNotNullStringFromJSON(jo,"statusCd","解析");
		String description = JsonUtil.getStringFromJSON(jo,"description","解析");
		String yearDt = JsonUtil.getStringFromJSON(jo,"yearDt","解析");
		String fundPlanId = JsonUtil.getNotNullStringFromJSON(jo,"fundPlanId","解析");
		String impleProjId = JsonUtil.getNotNullStringFromJSON(jo,"impleProjId","解析");
				
		// 判断资金计划是否已经存在
		FundPlan retFundPlan = fundPlanQuerySMO.queryFundPlan(fundPlanId);
		if(retFundPlan != null){
			throw new Exception("已经存在相同编码的资金计划！");
		}
		// 查询计划设置表 判断状态是否可填
		Map<String,Object> qrySetMap = new HashMap<String,Object>();
		qrySetMap.put("declareRelaType", MDA.DECLARE_RELA_TYPE_FUND_PLAN);
		qrySetMap.put("declareRelaObj", fundPlanId);
		List<PlanDeclareSet> setList = planDeclareSetQuerySMO.queryPlanDeclareSetList(qrySetMap);
		if(ListUtil.isListEmpty(setList)){
			throw new Exception("暂无设置该资金计划");
		}
		if(setList.size()>1){
			throw new Exception("该资金计划设置不正确");
		}
		if(!MDA.DECLAE_SET_DOC_YES.equals(setList.get(0).getDocCode())){
			throw new Exception("该资金计划不是可填阶段");
		}
		
		FundPlan fp = new FundPlan();
		fp.setDeclareSetId(setList.get(0).getDeclareSetId());
		fp.setFundPlanId(fundPlanId);
		fp.setFundPlanName(fundPlanName);
		fp.setImpleProjId(impleProjId);
		fp.setStatusCd(MDA.FUND_PLAN_STATUS_CD_APPLY+"");
		fp.setYearDt(yearDt);
		fp.setDocCode(docCode);
		fp.setCreateDt(new Date());
		fundPlanOperDAO.saveFundPlan(fp);
		
		
		// 保存 资金构成
		FundSource fs = new FundSource();
		JSONObject joVal = new JSONObject();
		String val = "";
		fs.setFundSourceObjType(MDA.FUND_SOURCE_OBJ_TYPE_FUND);
		fs.setFundSourceObj(impleProjId);
		fs.setStatusCd(Integer.parseInt(MDA.EFFECT_STATUS_CD));
		fs.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
		fs.setCreateDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
		
		if(jo.containsKey("bu")){
			joVal = jo.getJSONObject("bu");
			val = JsonUtil.getStringFromJSON(joVal,"金额","解析");
			fs.setFundSourceId(UUID.randomUUID().toString());
			fs.setFundSourceType(MDA.FUND_SOURCE_TYPE_BU);
			fs.setFundVal(val);
			fundSourceOperDAO.addFundSource(fs);
		}

		if(jo.containsKey("sheng")){
			joVal = jo.getJSONObject("sheng");
			val = JsonUtil.getStringFromJSON(joVal,"金额","解析");
			fs.setFundSourceId(UUID.randomUUID().toString());
			fs.setFundSourceType(MDA.FUND_SOURCE_TYPE_SHENG);
			fs.setFundVal(val);
			fundSourceOperDAO.addFundSource(fs);
		}
		
		if(jo.containsKey("gao")){
			joVal = jo.getJSONObject("gao");
			val = JsonUtil.getStringFromJSON(joVal,"金额","解析");
			fs.setFundSourceId(UUID.randomUUID().toString());
			fs.setFundSourceType(MDA.FUND_SOURCE_TYPE_GAO);
			fs.setFundVal(val);
			fundSourceOperDAO.addFundSource(fs);
		}
		
		if(jo.containsKey("shi")){
			joVal = jo.getJSONObject("shi");
			val = JsonUtil.getStringFromJSON(joVal,"金额","解析");
			fs.setFundSourceId(UUID.randomUUID().toString());
			fs.setFundSourceType(MDA.FUND_SOURCE_TYPE_SHI);
			fs.setFundVal(val);
			fundSourceOperDAO.addFundSource(fs);
		}
		
		if(jo.containsKey("qi")){
			joVal = jo.getJSONObject("qi");
			val = JsonUtil.getStringFromJSON(joVal,"金额","解析");
			fs.setFundSourceId(UUID.randomUUID().toString());
			fs.setFundSourceType(MDA.FUND_SOURCE_TYPE_QI);
			fs.setFundVal(val);
			fundSourceOperDAO.addFundSource(fs);
		}
		
	}

	
	@Override
	public void updateFundPlan(String jsonStr) throws Exception {
		
		JSONObject jo = JSONObject.fromObject(jsonStr);
		
		// 保存  planDeclareSet
		String fundPlanName = JsonUtil.getNotNullStringFromJSON(jo,"fundPlanName","解析");
		//String enterRequest = JsonUtil.getNotNullStringFromJSON(jo,"enterRequest","解析");
		String docCode = JsonUtil.getNotNullStringFromJSON(jo,"statusCd","解析");
		String description = JsonUtil.getStringFromJSON(jo,"description","解析");
		String fundPlanId = JsonUtil.getNotNullStringFromJSON(jo,"fundPlanId","解析");
		String impleProjId = JsonUtil.getNotNullStringFromJSON(jo,"impleProjId","解析");
				
		// 判断资金计划是否已经存在
		FundPlan retFundPlan = fundPlanQuerySMO.queryFundPlan(fundPlanId);
		if(retFundPlan == null){
			throw new Exception("不存在该资金计划，不能修改！");
		}
		
		retFundPlan.setVersion(new Date());
		//fundPlanOperDAO.updateFundPlan(fp);
	}


	@Override
	public void saveFundPlan(FundPlan fp) throws Exception {
		fundPlanOperDAO.saveFundPlan(fp);
	}

	@Override
	public void updateFundPlan(FundPlanListRespVo reqVo) throws Exception{
		FundPlan fundPlan = new FundPlan();
		PropertyUtils.copyProperties(fundPlan, reqVo);
		fundPlan.setVersion(new Date());
		fundPlanOperDAO.updateFundPlan(fundPlan);
	}


	@Override
	public void updatefundPlanForSet(FundPlan fundPlan) throws Exception {
		fundPlanOperDAO.updatefundPlanForSet(fundPlan);
	}


	@Override
	public void deletefundPlan(Map<String, Object> paramMap) throws Exception {
		fundPlanOperDAO.deletefundPlan(paramMap);		
	}


	@Override
	public void updateFundPlanForDel(FundPlanListRespVo reqVo) throws Exception{
		FundPlan fundPlan = new FundPlan();
		PropertyUtils.copyProperties(fundPlan, reqVo);
		fundPlanOperDAO.updateFundPlanForDel(fundPlan);		
	}



}
